using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using IQIGame.Onigao.Logic;
using LFloatMath.Math;
using System;
using System.Collections.Generic;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay
{
    public class UIHeroHeadItem : UIBaseNode
    {
        #region Template Generate,don't modify

        protected partial class UIB_UIHeroHeadItem
        {
            #region ObjectBinding Generate
            public UnityEngine.GameObject bigSkillBG { protected set; get; }
            public IQIGame.Onigao.Framework.ExImage icon { protected set; get; }
            public IQIGame.Onigao.Framework.ExText lv { protected set; get; }
            public IQIGame.Onigao.Framework.ExImage hp { protected set; get; }
            public IQIGame.Onigao.Framework.ExImage shield { protected set; get; }
            public IQIGame.Onigao.Framework.ExImage sp { protected set; get; }
            public IQIGame.Onigao.Framework.ExImage emotion { protected set; get; }
            public IQIGame.Onigao.Framework.ExImage heroClass { protected set; get; }
            public IQIGame.Onigao.Framework.ExImage heroElementType { protected set; get; }
            public UnityEngine.GameObject BuffIconPrefab { protected set; get; }
            public UnityEngine.GameObject bGTurn { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<UnityEngine.GameObject>("bigSkillBG", out var __tbv0);
                this.bigSkillBG = __tbv0;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExImage>("icon", out var __tbv1);
                this.icon = __tbv1;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("lv", out var __tbv2);
                this.lv = __tbv2;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExImage>("hp", out var __tbv3);
                this.hp = __tbv3;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExImage>("shield", out var __tbv4);
                this.shield = __tbv4;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExImage>("sp", out var __tbv5);
                this.sp = __tbv5;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExImage>("emotion", out var __tbv6);
                this.emotion = __tbv6;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExImage>("heroClass", out var __tbv7);
                this.heroClass = __tbv7;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExImage>("heroElementType", out var __tbv8);
                this.heroElementType = __tbv8;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("BuffIconPrefab", out var __tbv9);
                this.BuffIconPrefab = __tbv9;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("bGTurn", out var __tbv10);
                this.bGTurn = __tbv10;
            }
            #endregion ObjectBinding Generate
        }

        #endregion Template Generate,don't modify

        /// <summary>
        /// 可以自定义修改的
        /// </summary>
        protected partial class UIB_UIHeroHeadItem
        {
            public virtual void StartBinding(GameObject __go)
            {
                var binding = __go.GetComponent<ObjectBinding>();
                if (binding != null)
                {
                    this.InitBinding(binding);
                }
            }
        }

        protected UIB_UIHeroHeadItem ui { get; set; }

        private UIItemPool<UICommonImageItem> buffIconPool;
        private List<UICommonImageItem> buffList;
        private List<string> buffIconList;
        private UnitView unit;

        protected override void BeforeInit()
        {
            ui = new UIB_UIHeroHeadItem();
            ui.StartBinding(gameObject);
        }

        protected override void OnInit()
        {
            this.ui.BuffIconPrefab.SetActive(false);
            this.buffIconPool = this.InitPool<UICommonImageItem>(this.ui.BuffIconPrefab);
            this.buffList = new List<UICommonImageItem>();
            this.buffIconList = new List<string>();
        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {

        }

        protected override void OnDispose()
        {
        }

        private const string heroClassName = "HeroClass_";
        private const string heroElementTypeName = "HeroElementType_";

        public void Initialize(int nHeroID, int nLv, int nSkinID)
        {
            var rConfig = TableCenter.hero.Get(nHeroID);
            if (rConfig != null)
            {
                bool bHasClass = rConfig.HeroClass != 0;
                this.ui.heroClass.SetActive(bHasClass);
                if (bHasClass)
                {
                    var rClassName = heroClassName + rConfig.HeroClass;
                    this.ui.heroClass.sprite = this.resLoader.LoadABAsset<Sprite>(PathConstant.GetBattleSpritePath(rClassName));
                }
                bool bHasElement = rConfig.HeroElementType != 0;
                this.ui.heroElementType.SetActive(bHasElement);
                if (bHasElement)
                {
                    var rTypeName = heroElementTypeName + rConfig.HeroElementType;
                    this.ui.heroElementType.sprite = this.resLoader.LoadABAsset<Sprite>(PathConstant.GetBattleSpritePath(rTypeName));
                }
            }
            var rSkinConfig = TableCenter.heroSkin.Get(nSkinID);
            if (rSkinConfig != null)
            {
                var iconAssetPath = rSkinConfig.BattleStateHeadIcon_fullPath; // PathConstant.GetHeadPath(rSkinConfig.BattleStateHeadIcon);
                this.ui.icon.sprite = this.resLoader.LoadABAsset<Sprite>(iconAssetPath);
            }
            this.ui.lv.text = "LV." + nLv;
        }

        public void SetHp(LFloat nHp, LFloat nMaxHp, LFloat nShield)
        {
            var fHpRate = LFloat.zero;
            var fShieldRate = LFloat.zero;
            if (nHp + nShield <= nMaxHp)
            {
                fHpRate = nHp / nMaxHp;
                fShieldRate = (nShield + nHp) / nMaxHp;
            }
            else
            {
                fHpRate = nHp / (nMaxHp + nShield);
                fShieldRate = LFloat.one;
            }
            this.ui.hp.fillAmount = fHpRate.ToFloat();
            this.ui.shield.fillAmount = fShieldRate.ToFloat();
        }

        public void SetSp(LFloat nSp, LFloat nMaxSp)
        {
            this.ui.sp.fillAmount = nMaxSp > 0 ? (nSp / nMaxSp).ToFloat() : 0;
        }

        public void SyncBuff(List<BuffView> buffs)
        {
            this.buffIconList.Clear();
            int nLen = Math.Min(buffs.Count, 4);
            int num = 0;
            for (int i = 0; num < nLen && i < buffs.Count; i++)
            {
                var rConfig = TableCenter.buff.Get(buffs[i].buffID);
                if (rConfig != null && !string.IsNullOrEmpty(rConfig.Icon))
                {
                    this.buffIconList.Add(rConfig.Icon_fullPath);
                    num++;
                }
            }

            while (this.buffList.Count > num)
            {
                this.buffIconPool.HideOne(this.buffList[^1]);
                this.buffList.RemoveAt(this.buffList.Count - 1);
            }
            while (this.buffList.Count < num)
            {
                var rItem = this.buffIconPool.GetOne();
                this.buffList.Add(rItem);
            }

            for (int i = 0; i < num; i++)
            {
                var rItem = this.buffList[i];
                rItem.SetSprite(this.resLoader.LoadABAsset<Sprite>(this.buffIconList[i]));
            }
        }

        public void Refresh(UnitView rUnit)
        {
            var rAttrComp = rUnit.GetComponent<AttributeViewComponent>(ETComponentType.AttributeViewComponent);
            var rBuffComp = rUnit.GetComponent<BuffViewComponent>(ETComponentType.BuffViewComponent);
            var nHp = rAttrComp.Get(EAttributeType.Hp);
            var nMaxHp = rAttrComp.Get(EAttributeType.MaxHp);
            this.SetHp(nHp, nMaxHp, rBuffComp.shield.ToLFloat());
            this.SetSp(rAttrComp.Get(EAttributeType.SP), rAttrComp.Get(EAttributeType.MaxSP));
            this.SyncBuff(rBuffComp.buffs);

            var rSkillViewComp = rUnit.GetComponent<SkillViewComponent>(ETComponentType.SkillViewComponent);
            var bBigSkillCanCast = rSkillViewComp.allSkills.Exists(
                (x) => (1 << TableCenter.skill.Get(x).SkillType) == (int)ESkillType.BigSkill && rSkillViewComp.SkillCanCast(x, out _)
            );
            this.ui.bigSkillBG.SetActive(bBigSkillCanCast);
            this.unit = rUnit;
        }

        public void OnTurn(UnitView actUnit)
        {
            this.ui.bGTurn.SetActive(actUnit == this.unit);
        }
    }
}